package com.hongxun.budget.ui.activity.ys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.adapter.SpinnerAdapter;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.budget.utils.TestingUtils;
import com.hongxun.greenentity.tk.BDataDictionaryEntity;
import com.hongxun.greenentity.ys.EMaterialOldEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class YsEditOldMaterialActivity extends BaseActivity {

    @BindView(R.id.right_save)
    Button rightSave;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.unit)
    TextView unit;
    @BindView(R.id.count)
    EditText count;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.parameter)
    EditText parameter;
    EMaterialOldEntity d;
    int index = 0;
    SpinnerAdapter<BDataDictionaryEntity> spinnerAdapter;
    @Override
    public  int getLayoutId() {
        return R.layout.activity_edit_old_material;
    }

    @Override
    public  void initView(Bundle savedInstanceState) {
        Intent intent = getIntent();
        index = intent.getIntExtra("index", 0);
        d = (EMaterialOldEntity) intent.getSerializableExtra("material");
        if (d.getMBWork().equals("修理")) {
            findViewById(R.id.option_layout).setVisibility(View.GONE);
        }
        rightSave.setVisibility(View.VISIBLE);
        name.setText(d.getMBName());
        count.setText(d.getMBCount() + "");
        parameter.setText(d.getMBParameter());
        unit.setText("材料数量\\n(" + d.getMBUnit() + ")");
        List<BDataDictionaryEntity> entities = DBUtils.getBDataDictionaryEntities("opinion_type");
        spinner.setAdapter(spinnerAdapter = new SpinnerAdapter<BDataDictionaryEntity>(context, entities, spinner));
    }

    @OnClick(R.id.right_save)
    public void onViewClicked() {
        String c = getText(count);
        String n = getText(name);
        String p = getText(parameter);
        if (!TestingUtils.isEmpty(context, n, "名称")) return;
        if (!TestingUtils.isEmpty(context, p, "技术参数")) return;
        if (TextUtils.isEmpty(c)|| Double.parseDouble(c) == 0) {
            T.showLong(context, "数量不能为空或0");
            return;
        }
        d.setMBParameter(p);
        d.setMBName(n);
        d.setMBCount(Double.parseDouble(c));
        d.setMBDatastate("修改");
        d.setDROpinion(spinnerAdapter.getSelectedItem().getDKey());
        d.setDRWhere(d.getMBCount() + "");
        Intent intent = new Intent();
        intent.putExtra("material", d);
        intent.putExtra("index", index);
        setResult(Activity.RESULT_OK, intent);
        finish();
        T.showLong(context, "保存成功");
    }
}
